﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for TaskManagement.xaml
    /// </summary>
    public partial class TaskManagement : UserControl
    {
        string id = "0";
        public TaskManagement()
        {
            InitializeComponent();
            bindGrid();
            LoadSections();
        }

        private void LoadSections()
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.SectionList();
            cmbSectionName.DataContext = dt.DefaultView;
            cmbSectionName.DisplayMemberPath = dt.Columns["SectionName"].ToString();
            cmbSectionName.SelectedValuePath = dt.Columns["SectionId"].ToString();
        }

        private void clearAllFields()
        {
            txtTaskName.Text = "";
            txtDesc.Text = "";
            cmbSectionName.SelectedIndex = -1;
            rdoFeMale.IsChecked = false;
            rdoMale.IsChecked = true;
            rdo12Hr.IsChecked = false;
            rdo8Hr.IsChecked = true;
            rdo150.IsChecked = false;
            rdo133.IsChecked = false;
            rdo125.IsChecked = true;
            id = "0";
        }

        private void bindGrid()
        {
            DataTable dtTasks = new DataTable();
            dtTasks = AbhiruchiDAL.getTasks();
            gvTasks.ItemsSource = dtTasks;
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            clearAllFields();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            string parameter = string.Empty;
            parameter = GetTaskParameters();

            if (DoValidations())
            {
                if (id == "0")
                {
                    AbhiruchiDAL.setTaskInformation(txtTaskName.Text, txtDesc.Text, Convert.ToInt32(cmbSectionName.SelectedValue), parameter);
                    clearAllFields();
                    MessageBox.Show("Task Information Saved Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    AbhiruchiDAL.updateTaskInformation(Convert.ToInt32(id), txtTaskName.Text, txtDesc.Text, Convert.ToInt32(cmbSectionName.SelectedValue), parameter);
                    clearAllFields();
                    MessageBox.Show("Task Information Modified Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    id = "0";
                }
                bindGrid();
            }
        }

        private string GetTaskParameters()
        {
            string s = string.Empty, gender = string.Empty, hour = string.Empty, pay = string.Empty;
            if (rdoMale.IsChecked == true)
            {
                gender = "Male";
            }
            else
            {
                gender = "Female";
            }

            if (rdo8Hr.IsChecked == true)
            {
                hour = "8";
            }
            else
            {
                hour = "12";
            }

            if (rdo125.IsChecked == true)
            {
                pay = "125";
            }
            else if (rdo133.IsChecked == true)
            {
                pay  = "133";
            }
            else if (rdo150.IsChecked == true)
            {
                pay = "150";
            }

            s = gender + "_" + hour + "_" + pay;
            return s;
        }

        private bool DoValidations()
        {
            if (txtTaskName.Text == "")
            {
                MessageBox.Show("Please Enter Task Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            else if (cmbSectionName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select One Section", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.CompareTaskName(txtTaskName.Text.ToString());
            if (!(dt.Rows[0][0].ToString() == "0"))
            {
                txtTaskName.Text = "";
                txtTaskName.Focus();
                MessageBox.Show("Please Give Diffrent Task Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        private void gvTasks_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            id = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            txtTaskName.Text=((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtDesc.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            cmbSectionName.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();
            SetTaskParameters(((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString());
        }

        private void SetTaskParameters(string s)
        {
            string[] arr = s.Split('_');
            if (arr[0] == "Male")
            {
                rdoFeMale.IsChecked = false;
                rdoMale.IsChecked = true;  
            }
            else if (arr[0] == "Female")
            {
                rdoMale.IsChecked = false;
                rdoFeMale.IsChecked = true;
            }

            if (arr[1] == "8")
            {
                rdo12Hr.IsChecked = false;
                rdo8Hr.IsChecked = true;
            }
            else if (arr[1] == "12")
            {
                rdo8Hr.IsChecked = false;
                rdo12Hr.IsChecked = true;
            }

            if (arr[2] == "125")
            {
                rdo150.IsChecked = false;
                rdo133.IsChecked = false;
                rdo125.IsChecked = true;
            }
            else if (arr[2] == "133")
            {
                rdo150.IsChecked = false;
                rdo125.IsChecked = false;
                rdo133.IsChecked = true;
            }
            else if (arr[2] == "150")
            {
                rdo125.IsChecked = false;
                rdo133.IsChecked = false;
                rdo150.IsChecked = true;
            }
            
        }

        private void cmbSectionName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbSectionName.SelectedIndex != -1)
            {
                int secId = Convert.ToInt32(cmbSectionName.SelectedValue);
                DataTable dt = new DataTable();
                dt = AbhiruchiDAL.getMaxTaskId(secId);
                txtTaskName.Text = ((System.Data.DataRowView)(cmbSectionName.SelectedItem)).Row[1].ToString() + "_" + dt.Rows[0][0].ToString();
            }
        }

        //private void txtTaskName_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    DataTable dt = new DataTable();
        //    dt = AbhiruchiDAL.CompareTaskName(txtTaskName.Text.ToString());
        //    if (!(dt.Rows[0][0].ToString() == "0"))
        //    {
        //        txtTaskName.Text = "";
        //        txtTaskName.Focus();
        //        MessageBox.Show("Please Give Diffrent Task Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        //    }
            
        //}
    }
}
